package com.wisevar.thirdplatform.gateway.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.stream.Collectors;


@Configuration
@Slf4j
public class RedissonConfig {

    @Autowired
    private RedisProperties redisProperties;

    public static final String REDIS_PROTOCAL_PREFIX = "redis://";

    @Bean
    public RedissonClient redissonClient() {
        log.info("Init redissonClient start .....");
        Config config = new Config();
        List<String> nodeList = CollUtil.emptyIfNull(StrUtil.split(redisProperties.getServers(), ",")).stream().map(e -> REDIS_PROTOCAL_PREFIX + e).collect(Collectors.toList());
        config.useSentinelServers()
                .setMasterName(redisProperties.getMasterName())
                .setDatabase(redisProperties.getDatabase())
                .setPassword(redisProperties.getPassword())
                .setCheckSentinelsList(false)
                .addSentinelAddress(nodeList.toArray(new String[]{}));
        log.info("Init redissonClient success .....");
        return Redisson.create(config);
    }

}
